define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'choi/star/index' + location.search,
                    add_url: 'choi/star/add',
                    edit_url: 'choi/star/edit',
                    del_url: 'choi/star/del',
                    multi_url: 'choi/star/multi',
                    table: 'star',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                commonSearch: false,
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('Username')},
                        {field: 'position', title: __('Position')},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'year', title: __('Year')},
                        {field: 'quarterdata', title: __('Quarterdata'), searchList: {"Q1":__('Q1'),"Q2":__('Q2'),"Q3":__('Q3'),"Q4":__('Q4')}, formatter: Table.api.formatter.normal},
                        {field: 'description', title: __('Description')},
                        {field: 'onlineswitch', title: __('Onlineswitch'), searchList: {"0":__('Onlineswitch 0'),"1":__('Onlineswitch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'tagswitch', title: __('Tagswitch'), operate: false, searchList: {"0":__('Tagswitch 0'),"1":__('Tagswitch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //标签切换
            $('#toolbar').on('click', '.btn-tag', function () {
                console.log('~~~~~~~~~~~');
                $.ajax({
                    url: 'choi/star/tag',
                    method: 'GET',
                    success: function (res) {
                        console.log(res)
                        if (res) {
                            $('#toolbar .btn-tag i').removeClass('text-red');
                            $('#toolbar .btn-tag i').removeClass('fa-flip-horizontal');
                            $('#toolbar .btn-tag i').addClass('text-success');
                        } else {
                            $('#toolbar .btn-tag i').removeClass('text-success');
                            $('#toolbar .btn-tag i').addClass('text-red');
                            $('#toolbar .btn-tag i').addClass('fa-flip-horizontal');
                        }
                        layer.msg("操作成功")
                    }
                })
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});